package com.example.printer2.common;

import android.content.ActivityNotFoundException;
import android.content.Context;
import android.content.Intent;
import android.os.Build;

import androidx.activity.result.contract.ActivityResultContract;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

public class ResultContract extends ActivityResultContract<Boolean, Intent> {
    public static final String DOC = "application/msword";
    public static final String DOCX = "application/vnd.openxmlformats-officedocument.wordprocessingml.document";
    public static final String PDF = "application/pdf";

    public Intent Intent()
    {
        try {
            Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
            intent.addCategory(Intent.CATEGORY_OPENABLE);
            //设置doc,docx,ppt,pptx,pdf 5种类型
            intent.setType("application/msword|application/vnd.openxmlformats-officedocument.wordprocessingml.document" +
                    "|application/pdf");
            //在API>=19之后设置多个类型采用以下方式，setType不再支持多个类型
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
                intent.putExtra(Intent.EXTRA_MIME_TYPES,
                        new String[]{DOC,DOCX,PDF});
            }
            return intent;
        } catch (ActivityNotFoundException e) {

        }
        return null;
    }
    @NonNull
    @Override
    public Intent createIntent(@NonNull Context context, Boolean input) {
        Intent intent = new Intent();
        intent.setType(DOC+"|"+DOCX+"|"+PDF);//同时选择视频和图片
        intent.setAction(Intent.ACTION_GET_CONTENT);
        //  Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        return intent;
    }


    @Override
    public Intent parseResult(int resultCode, @Nullable Intent intent) {
        return intent;
    }

}

